package gold.digger;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC816 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<String> ambiguousCoordinates(String str) {
            List<String> res = new ArrayList<>();
            boolean[] beforeAllZero = new boolean[str.length()];
            boolean[] afterAllZero = new boolean[str.length()];
            beforeAllZero[1] = str.charAt(1) == '0';
            afterAllZero[str.length() - 3] = str.charAt(str.length() - 2) == '0';

            for (int i = 2; i < str.length() - 1; i++) {
                beforeAllZero[i] = str.charAt(i) == '0' && beforeAllZero[i - 1];
            }

            for (int i = str.length() - 4; i >= 1; i--) {
                afterAllZero[i] = str.charAt(i + 1) == '0' && afterAllZero[i + 1];
            }

            for (int i = 1; i < str.length() - 2; i++) {
                // 逗号加在位置i之后
                if (beforeAllZero[i] && i > 1) continue;
                if (afterAllZero[i] && i <= str.length() - 4) continue;
                List<String> leftPart = getNumWithDot(str, 1, i);
                List<String> rightPart = getNumWithDot(str, i + 1, str.length() - 2);

                // do join
                for (String leftVal : leftPart) {
                    for (String rightVal : rightPart) {
                        res.add("(" + leftVal + ", " + rightVal + ")");
                    }
                }
            }

            return res;
        }

        public List<String> getNumWithDot(String str, int start, int end) {
            List<String> partRes = new ArrayList<>();
            if (String.valueOf(Integer.parseInt(str.substring(start, end + 1))).length() == end - start + 1)
                partRes.add(str.substring(start, end + 1));   // 不加小数点的

            // 后面都是加小数点的
            if ('0' == str.charAt(end)) return partRes;
            for (int i = start; i < end; i++) {
                if (i > start && str.charAt(i - 1) == '0' && str.charAt(start) == '0') break;
                if (0 == Integer.parseInt(str.substring(start, i + 1)) && i - start >= 1) break;
                partRes.add(str.substring(start, i + 1) + "." + str.substring(i + 1, end + 1));
            }

            return partRes;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.ambiguousCoordinates("(123)"));
        System.out.println(solution.ambiguousCoordinates("(00011)"));
        System.out.println(solution.ambiguousCoordinates("(0123)"));
        System.out.println(solution.ambiguousCoordinates("(100)"));

        System.out.println(solution.ambiguousCoordinates("(01001)"));
    }

    public static void main(String[] args) throws Exception {
        LC816 an = new LC816();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
